/*
 * Copyright (C) 2017 Honeywell, Inc. All Rights Reserved.
 */

package org.thanos.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.thanos.demo.config.RabbitTopic;
import org.thanos.demo.message.Producer;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;


@Component
public class RabbitMqAppListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private Producer producer;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        CompletableFuture.runAsync(() -> {
            for (; ; ) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    producer.send(RabbitTopic.DEMO_TOPIC, UUID.randomUUID().toString());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
